/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.infrastructure.rules;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * An abstract rule class that can evaluate properties of objects.
 * @param <TValue> The type of the property to be evaluated.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public abstract class ValueBoundRule<TValue> extends CompositeRule {
    private String attributeName;
    private Object candidate;

    /**
     * ValueBoundRule constructor that receive the candidate and the attributeName.
     * @param candidate The object to be validated.
     * @param attributeName The property name to validate.
     */
    public ValueBoundRule(Object candidate, String attributeName) {
        this.attributeName = attributeName;
        this.candidate = candidate;
    }

    /**
     * The attributeName is the object property to be evaluated in the rule.
     * @return The attributeName.
     */
    public String getAttributeName() {
        return attributeName;
    }

    /**
     * Function that retrieve the value of object's property and convert it to string.
     * @return A string value representing the property of the object.
     */
    protected String getCandidateStringValue() {
        return this.getCandidateObjectValue().toString();
    }

    /**
     * Function that retrieve the value of object's property as TValue.
     * @return A TValue representing the property of the object.
     */
    protected TValue getCandidateTValue() {
        return (TValue) this.getCandidateObjectValue();
    }

    /**
     * Function that retrieve the value of object's property as Object.
     * @return A Object representing the property of the object.
     */
    private Object getCandidateObjectValue() {
        Object result = 0;
        try {
            String prop = Character.toUpperCase(attributeName.charAt(0)) + attributeName.substring(1);
            String mname = "get" + prop;
            Class<?>[] types = new Class[]{};
            Method method = candidate.getClass().getMethod(mname, types);
            result = method.invoke(candidate, new Object[0]);

        } catch (IllegalAccessException ex) {
//            Logger.getLogger(ValueBoundRule.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
//            Logger.getLogger(ValueBoundRule.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
//            Logger.getLogger(ValueBoundRule.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
//            Logger.getLogger(ValueBoundRule.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
//            Logger.getLogger(ValueBoundRule.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    /**
     * Function that retrieve the class name of the candidate object.
     * @return The class name as string.
     */
    protected String getCandidateName() {
        return candidate.getClass().getSimpleName();
    }

    public abstract boolean isValid();
}
